package com.classroom.smart.controller;

import com.classroom.smart.pojo.User;
import com.classroom.smart.result.CommonResult;
import com.classroom.smart.pojo.Teacher;
import com.classroom.smart.service.TeacherService;
import com.classroom.smart.vo.LoginVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Auther:YouGuoyun
 * @Title:leetcode
 * @Date:Create in 2023/2/23 19:59
 **/

@Controller
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    //根据条件分页查询用户列表
    @GetMapping("viewTeacher")
    @ResponseBody
    public CommonResult viewAllTeacher(
            @RequestParam(value = "current",required = false,defaultValue = "1")int current,
            @RequestParam(value = "query",required = false,defaultValue = "")String query
            ){
        List<Teacher> teachers = teacherService.viewTeacherByQuery(current,query);
        CommonResult commonResult = null;
        if (teachers != null && teachers.size()!= 0){
            PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("pageInfo",pageInfo);
            hashMap.put("query",query);
            commonResult = new CommonResult(200,"查询成功",hashMap);
        }else {
            commonResult = new CommonResult(455,"查询失败");
        }
        return commonResult;
    }

    //删除用户信息  @PathVariable可以解析路径变量，value属性与{Key}与Key一致即可
    @DeleteMapping("deleteTeacher/{teacherId}")
    @ResponseBody
    public CommonResult del(@PathVariable("teacherId") int teacherId){
        CommonResult commonResult = null;
        boolean flag = teacherService.delete(teacherId);
        if(flag){
            commonResult = new CommonResult(200 , "删除成功" , flag);
        }else{
            commonResult = new CommonResult(499 , "删除失败");
        }
        return commonResult;
    }

    //根据用户ID获取要更新的用户的信息
    @GetMapping("getUpdateTeacher/{teacherId}")
    @ResponseBody
    public CommonResult getUpdatas(@PathVariable int teacherId){
        CommonResult commonResult = null;
        Teacher teacher = teacherService.viewOne(teacherId);
        if (teacher != null){
            commonResult = new CommonResult(200,"查询成功",teacher);
        }else {
            commonResult = new CommonResult(455,"查询失败");
        }
        return commonResult;
    }

    //更新用户信息
    @PutMapping("updateTeacher")
    @ResponseBody
    public CommonResult updataUser(@RequestBody Teacher teacher){
        CommonResult commonResult = null;
        boolean flag  = teacherService.update(teacher);
        if (flag){
            commonResult = new CommonResult(200,"更新成功",flag);
        }else {
            commonResult = new CommonResult(455,"更新失败");
        }
        return commonResult;
    }

    //添加用户
    @PostMapping("addTeacher")
    @ResponseBody
    public CommonResult addTeacher(@RequestBody Teacher teacher){
        CommonResult commonResult = null;
        boolean flag = teacherService.addOne(teacher);
        if (flag){
            commonResult = new CommonResult(200,"添加成功",flag);
        }else {
            commonResult = new CommonResult(499,"添加失败");
        }
        return commonResult;
    }


}
